package com.zjt.swipeview.demo.customview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zjt.swipeview.demo.R;
import com.zjt.swipeview.event.SwipeLoadMoreTrigger;
import com.zjt.swipeview.event.SwipeTrigger;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <P>Company: </p>
 *
 * @author zhengjianting
 * @time 2017/11/24 0024 上午 11:38
 */

public class LoadMoreFooter extends LinearLayout implements SwipeTrigger, SwipeLoadMoreTrigger {

    private ImageView footerProgress;
    private TextView footerText;
    private AnimationDrawable loadMoreAnim;

    public LoadMoreFooter(Context context) {
        this(context, null);
    }

    public LoadMoreFooter(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreFooter(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        footerProgress = (ImageView) this.findViewById(R.id.footerProgressBar);
        footerText = (TextView) this.findViewById(R.id.footerText);

        loadMoreAnim = (AnimationDrawable) footerProgress.getDrawable();
        loadMoreAnim.start();
    }

    @Override
    public void onPrepare() {
        footerText.setText("上拉加载更多...");
    }

    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (-y >= getHeight() && !isComplete) {
            footerText.setText("松开加载...");
        }
    }

    @Override
    public void onRelease() {
    }

    @Override
    public void onComplete() {
        footerProgress.setVisibility(GONE);
        loadMoreAnim.stop();
        footerText.setText("加载完成");
    }

    @Override
    public void onReset() {
        footerText.setText("上拉加载更多...");
    }

    @Override
    public void onLoadMore() {
        footerProgress.setVisibility(VISIBLE);
        loadMoreAnim.start();
        footerText.setText("加载中...");
    }
}
